/*
    Movino-S60
    Copyright 2006, 2007 Martin Storsj�

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2.1 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __JPEGENCODER_H
#define __JPEGENCODER_H

#include <e32base.h>

#include <stdio.h>
extern "C" {
#include <jpeglib.h>
#include <jerror.h>
}
#include <setjmp.h>


class CJpegEncoder : public CBase {
public:
	CJpegEncoder(TInt aQuality);
	~CJpegEncoder();

	TDesC8& SetQuality(TInt aQuality);

	TDesC8& Encode(TDesC8& aData, TInt aWidth, TInt aHeight);

	TDesC& GetErrorString();

private:
	struct jpeg_compress_struct iCinfo;
	struct jpeg_error_mgr iJerr;
	struct jpeg_destination_mgr iDestMgr;

	jmp_buf iReturnPoint;
	TUint8 *iWriteBuffer;
	TInt iWriteBufferSize;

	TInt iWritten;
	char iMessageBuffer[JMSG_LENGTH_MAX+1];
	TBuf<JMSG_LENGTH_MAX> iMessageDesc;
	TPtrC8 iEmptyDesc;
	TPtrC8 iDataDesc;

	TInt iPlaneHeight;
	JSAMPARRAY iYPlane;
	JSAMPARRAY iCbPlane;
	JSAMPARRAY iCrPlane;

	static void error_exit(j_common_ptr cinfo);
	static void output_message(j_common_ptr cinfo);

	static void init_destination(j_compress_ptr cinfo);
	static boolean empty_output_buffer(j_compress_ptr cinfo);
	static void term_destination(j_compress_ptr cinfo);
};



#endif
